package stack;

import java.util.ArrayList;
import java.util.Arrays;

public class MyStack {

    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[5];
    }

    public void push(int ele){
        if(isFull()){
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = ele;
    }
    public int size(){
        return elem.length;
    }
    private boolean isFull(){
        return usedSize==elem.length;
    }
    public boolean empty(){
        return usedSize==0;
    }

    public int pop(){
        if(empty()){
            throw new RuntimeException("stack is empty");
        }
        int ret = this.elem[usedSize-1];
        usedSize--;
        //this.ele[usedSize-1] = null;如果是引用类型，则需要引用置空
        return ret;
    }

    public int peak(){
        if(empty()){
            throw new RuntimeException("stack is empty");
        }
        int ret = this.elem[usedSize-1];
        return ret;
    }


}
